package com.project.controller;


import com.project.dto.RiderDto;
import com.project.dto.UserDto;
import com.project.entity.Customer;
import com.project.entity.Rider;
import com.project.entity.User;
import com.project.mapper.TCustomerMapper;
import com.project.mapper.TOrderMapper;
import com.project.service.ICustomRemoteService;
import com.project.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangyang
 * @since 2021-10-06
 */
@RestController
@RequestMapping("/customer")
public class TCustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private TCustomerMapper mapper;
    @Autowired
    private TOrderMapper orderMapper;
    @Autowired
    private ICustomRemoteService service;

//    @Scheduled(cron = "30 * * * * *")//每分钟的第五秒执行以下代码
//    public void add(){
//        System.out.println("111111111111111111111");
//        List<UserDto> dto = service.findAllByRemote();
//        List<Customer> userList = new ArrayList<>();
//        for (UserDto userDto : dto) {
//            Customer customer = new Customer();
//            customer.setCode(userDto.getUserCode());
//            customer.setRealName(userDto.getRealName());
//            customer.setPhone(userDto.getPhone());
//            customer.setSex(userDto.getGentle());
//            userList.add(customer);
//        }
//
//        List<Customer> byItem = findByItem(null, null);
//        A: for (int i = 0; i < userList.size(); i++) {
//            String code = userList.get(i).getCode();
//
//            for (int i1 = 0; i1 < byItem.size(); i1++) {
//                String code1 = byItem.get(i1).getCode();
//                if (code.equals(code1)){
//                    continue A;
//                }
//            }
//            mapper.insert(userList.get(i));
//        }
//    }

    @GetMapping("/findByItem")
    public List<Customer> findByItem(String realName, String phone) {
        List<Customer> byItem = customerService.findByItem(realName, phone);
        for (int i = 0; i < byItem.size(); i++) {
            Customer customer = byItem.get(i);
            Customer customer1 = orderMapper.findCustom(customer.getCode());
            customer.setOrderNum(customer1.getOrderNum());
            customer.setOrderMoney(customer1.getOrderMoney());
            byItem.set(i, customer);
        }
        return byItem;
    }

    @GetMapping("/findById")
    public Customer findById(String id) {
        return customerService.findById(id);
    }
}
